unit specexport;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, StrUtils;

type
  TfrmSpecExport = class(TForm)
    edtSrcDest: TEdit;
    Label1: TLabel;
    btnBrowse: TButton;
    dlgSaveFile: TSaveDialog;
    btnExport: TButton;
    btnExit: TButton;
    lblExportComplete: TLabel;
    procedure btnBrowseClick(Sender: TObject);
    procedure btnExitClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure btnExportClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  frmSpecExport: TfrmSpecExport;
  lvOutputDest : string;
  lvDestFile : string;

implementation

uses main;

{$R *.dfm}

procedure TfrmSpecExport.btnBrowseClick(Sender: TObject);
begin
  dlgSaveFile.Execute;
  lvOutputDest := dlgSaveFile.FileName;
  if lvOutputDest <> '' then
  begin
    if not AnsiEndsStr(lvOutputDest, '.csv') then
      lvOutputDest := lvOutputDest + '.csv';

    edtSrcDest.Text := lvOutputDest;
  end;
end;

procedure TfrmSpecExport.btnExitClick(Sender: TObject);
begin
  lvOutputDest := '';
  self.Close;
end;

procedure TfrmSpecExport.btnExportClick(Sender: TObject);
var
  lvDestFile : TextFile;
begin
  try
    Assignfile(lvDestFile, lvOutputDest);
    ReWrite(lvDestFile);

    Write(lvDestFile, Format('Specification for:, %s', [frmMain.tblSpecsSpecName.AsString]));
    WriteLn(lvDestFile);
    Write(lvDestFile, 'Field,Start,Size');
    WriteLn(lvDestFile);

    Write(lvDestFile, Format('Part Number, %s,%s', [IntToStr(frmMain.tblSpecsPNumStart.AsInteger), IntToStr(frmMain.tblSpecsPNumSize.AsInteger)]));
    WriteLn(lvDestFile);

    if frmMain.tblSpecsPDescEnabled.AsBoolean then
    begin
      Write(lvDestFile, Format('Part Description, %s,%s', [IntToStr(frmMain.tblSpecsPDescStart.AsInteger), IntToStr(frmMain.tblSpecsPDescSize.AsInteger)]));
      WriteLn(lvDestFile);
    end;

    if frmMain.tblSpecsPSupEnabled.AsBoolean then
    begin
      Write(lvDestFile, Format('Supersession, %s,%s', [IntToStr(frmMain.tblSpecsPSupStart.AsInteger), IntToStr(frmMain.tblSpecsPSupSize.AsInteger)]));
      WriteLn(lvDestFile);
    end;

    if frmMain.tblSpecsPGrpEnabled.AsBoolean then
    begin
      Write(lvDestFile, Format('Group Code, %s,%s', [IntToStr(frmMain.tblSpecsPGrpStart.AsInteger), IntToStr(frmMain.tblSpecsPGrpSize.AsInteger)]));
      WriteLn(lvDestFile);
    end;

    if frmMain.tblSpecsPMinOrdEnabled.AsBoolean then
    begin
      Write(lvDestFile, Format('Minimum Order, %s,%s', [IntToStr(frmMain.tblSpecsPMinOrdStart.AsInteger), IntToStr(frmMain.tblSpecsPMinOrdSize.AsInteger)]));
      WriteLn(lvDestFile);
    end;

    if frmMain.tblSpecsPRetEnabled.AsBoolean then
    begin
      Write(lvDestFile, Format('Retail Price, %s,%s', [IntToStr(frmMain.tblSpecsPRetStart.AsInteger), IntToStr(frmMain.tblSpecsPRetSize.AsInteger)]));
      WriteLn(lvDestFile);
    end;

    if frmMain.tblSpecsPCostEnabled.AsBoolean then
    begin
      Write(lvDestFile, Format('Cose Price, %s,%s', [IntToStr(frmMain.tblSpecsPCostStart.AsInteger), IntToStr(frmMain.tblSpecsPCostSize.AsInteger)]));
      WriteLn(lvDestFile);
    end;

    if frmMain.tblSpecsPSurcEnabled.AsBoolean then
    begin
      Write(lvDestFile, Format('Part Surcharge, %s,%s', [IntToStr(frmMain.tblSpecsPSurcStart.AsInteger), IntToStr(frmMain.tblSpecsPSurcSize.AsInteger)]));
      WriteLn(lvDestFile);
    end;

    if frmMain.tblSpecsPDiscEnabled.AsBoolean then
    begin
      Write(lvDestFile, Format('Discount Code, %s,%s', [IntToStr(frmMain.tblSpecsPDiscStart.AsInteger), IntToStr(frmMain.tblSpecsPDiscSize.AsInteger)]));
      WriteLn(lvDestFile);
    end;

    if frmMain.tblSpecsPUOIEnabled.AsBoolean then
    begin
      Write(lvDestFile, Format('UOI, %s,%s', [IntToStr(frmMain.tblSpecsPUOIStart.AsInteger), IntToStr(frmMain.tblSpecsPUOISize.AsInteger)]));
      WriteLn(lvDestFile);
    end;

    if frmMain.tblSpecsPWeightEnabled.AsBoolean then
    begin
      Write(lvDestFile, Format('Part Weight, %s,%s', [IntToStr(frmMain.tblSpecsPWeightStart.AsInteger), IntToStr(frmMain.tblSpecsPWeightSize.AsInteger)]));
      WriteLn(lvDestFile);
    end;

    if frmMain.tblSpecsPSuppEnabled.AsBoolean then
    begin
      Write(lvDestFile, Format('Supplier, %s,%s', [IntToStr(frmMain.tblSpecsPSuppStart.AsInteger), IntToStr(frmMain.tblSpecsPSuppSize.AsInteger)]));
      WriteLn(lvDestFile);
    end;

    CloseFile(lvDestFile);
    lblExportComplete.Visible := True;
  Except
    on E : Exception do
    begin
      ShowMessage('Error writing to: ' + lvOutputDest + #13#10 + #13#10 + E.Message + #13#10 + #13#10 +
                  'Please ensure you have full write access to the selected' +
                  ' location, if you continue to have problems, try running GIPM as an administrator.');
    end;
  end;
end;

procedure TfrmSpecExport.FormCreate(Sender: TObject);
begin
  lvOutputDest := 'C:\' + frmMain.tblSpecsSpecName.AsString + '.csv';
  edtSrcDest.Text := lvOutputDest;
end;

end.
